Hi eveyone
Question is :-
String repeat(String s, int n) that creates and returns a new string that is made by concatenating
n copies of the parameter string s. For example, calling this method with the parameters “Hello”
and 3 would return the string “HelloHelloHello”. If n equals zero, the method should return the
empty string.
I am a beginner in c and trying my hand at recursion .This code doesn't work.
Please guide me and also tell any clever/advance/optimise steps that i can use to code as a pro.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char* Repeat(char *s,int n)
{
char *c;
char *e;
if(n==0)
{
return " ";
}
if(n==1)
{
c=strcpy(c,s);
return c;
}
if(n>1)
{
e=Repeat(s,n-1);
e=strcat(e,s);
return e;
}
}
void main()
{
char a[]="Hello";
char *b;
int n;
printf("Enter the no of times u want to repeat\n");
scanf("%d",&n);
b= Repeat(a,n);
printf("%s",b);
getch();
}